package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.IRoleController;
import zxq.rollcall.course.entity.SystemRoleTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.RoleDTO;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.entity.query.RoleQuery;
import zxq.rollcall.course.service.MPSystemRoleTableService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/role")
@Api(tags = "系统角色表")
public class SystemRoleTableAction implements IRoleController {

    @Autowired
    MPSystemRoleTableService mpSystemRoleTableService;

    @Override
    public ResultDTO<Boolean> save(RoleQuery roleQuery) {
        SystemRoleTableEntity roleTableEntity = new SystemRoleTableEntity();
        BeanUtils.copyProperties(roleQuery,roleTableEntity);
        Boolean rst = mpSystemRoleTableService.save(roleTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"插入成功",true);
        }
        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> update(Integer id, RoleQuery roleQuery) {
        SystemRoleTableEntity systemRoleTableEntity = new SystemRoleTableEntity();
        BeanUtils.copyProperties(roleQuery,systemRoleTableEntity);
        systemRoleTableEntity.setId(id);
        Boolean rst = mpSystemRoleTableService.updateById(systemRoleTableEntity);
        if(rst) {
           return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        Boolean rst = mpSystemRoleTableService.removeById(id);
        if(rst) {
            return new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<List<RoleDTO>> query(PageQuery pageQuery) {
        Page<SystemRoleTableEntity> page = new Page<>(pageQuery.getPage(),pageQuery.getCount());
        List<RoleDTO> roleDTOS = mpSystemRoleTableService.page(page).getRecords().stream().map(item -> {
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(item,roleDTO);
            return roleDTO;
        }).collect(Collectors.toList());
        return new ResultDTO<>(1,"查询成功",roleDTOS);
    }

}

